import sys, math, random
import numpy as np
import scipy as sp
import crop
import util


def generate_matrix(n, r) :
    global A,B, C_mat, C, p_dist, C, h1, h2
    C, h1, h2 = {}, {},{}
#    p_dist = np.zeros((p,), dtype=np.int)
    A = np.random.randint(r, size=(n, n))
    B = np.random.randint(r, size=(n, n))
    C_mat = [[0 for i in range(n)] for x in range(n)]

def matrix_mult(A, B) :
    return A * B

n = 100
generate_matrix(n, 4)
#print (A)
#print (B)

def callback(*args, **kwargs):
    print (args, kwargs)

crop
with util.Timer() as t :
    for idx in range(n) :
        crop.crop(A[idx],B[idx])
print('CRoP took %.03f sec.' % t.interval)

# build result matrix from C
for key, val in C.iteritems():
    try :
        C_mat[key[0]][key[1]] = val
    except Exception as e :
        print e
        print key, val

#print "p_dist: ", p_dist
#print "avg: ", np.average(p_dist)
#print "mean: ", np.mean(p_dist)
#print "std: ", np.std(p_dist)

A = np.mat(A)
B = np.mat(B)
COUT = matrix_mult(A,B)
C_mat = np.mat(C_mat)

print (np.array_equal(COUT, C_mat))